import urllib2, json


_url_video = "http://10.10.17.163:8000/api/v1/video/";
_url_shot = "http://10.10.17.163:8000/api/v1/shot/";
def video_create(src, callback_url):
    req = urllib2.Request(_url_video,
                    headers = {
                        "Content-Type": "application/json",
                    },
                    data = json.dumps(dict(src=src,callback_url=callback_url),),
                    )
    f = urllib2.urlopen(req)
    assert(f.getcode() == 201)
    szid = f.headers['location'].rsplit('/',2)[-2]
    return szid

def video_read(szid):
    szurl = '%(_url_video)s%(szid)s/?format=json'%dict(_url_video=_url_video,szid=szid,)
    f = urllib2.urlopen(szurl)
    s = f.read()
    return json.loads(s)

def shot_list(szid):
    szurl = _url_shot + '?format=json&limit=0&video__id='+szid
    
    #'%(s)s%?format=json&video__id=%(szid)s&limit=0'%dict(s=_url_shot,szid=szid,)
    f = urllib2.urlopen(szurl)
    s = f.read()
    return json.loads(s)

if __name__ == '__main__':
    #szid='271'
    #video_create(src='in/pulsar.mpg')

    import glob
    l = glob.glob('c:/t/in/*.*')
    l = map(lambda s:s.replace('c:/t',''),l)
    l = map(lambda s:s.replace('\\','/'),l)
    l = map(lambda s:s.strip('/'),l)
    print l

    callback_url = 'http://chosun.com'
    for src in l:
       szid = video_create(src, callback_url)


    print video_read(szid=szid)    
    print shot_list(szid=szid)

       